﻿using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class WasteWin : Form
    {
        string plCode = "";
        string dataType = "";
        cMethods.DBHelper db;
        public WasteWin(string pCode, string dType)
        {
            InitializeComponent();
            plCode = pCode;
            dataType = dType;
            db = new cMethods.DBHelper();
        }

        private void WasteWin_Load(object sender, EventArgs e)
        {
            string title = "不合格品录入";
            if (dataType == "waste")
            {
                title = "废料录入";
            }
            this.Text = title;
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {    
                if (string.IsNullOrWhiteSpace(plCode))
                {
                    MessageBox.Show("计划编码获取失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (string.IsNullOrWhiteSpace(tbNumber.Text))
                {
                    MessageBox.Show("数量不允许为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    if (float.Parse(tbNumber.Text) < 0)
                    {
                        MessageBox.Show("数量输入有误", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                StringBuilder sb = new StringBuilder();
                sb.Append("insert into pl_ProductionWaste_info(plCode,");
                sb.Append("number,unit,createuser,createtime,happenDate,dType) values(");
                sb.Append("'" + plCode + "','" + tbNumber.Text + "','Kg','" + CommonMethods.userCode + "',");
                sb.Append("'" + DateTime.Now.ToString() + "','" + dtpDate.Text + "','" + dataType + "')");
                var r = db.UpdateData(sb.ToString());
                if (int.Parse(r) > 0)
                {
                    MessageBox.Show("添加成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("添加失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }
    }
}
